package com.learn.proxy;

import com.learn.proxy.Data;
import com.learn.proxy.UnifiedInterface;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>项目名称: design-pattern </p>
 * <p>文件名称: Proxy </p>
 * <p>描述: 代理类 </p>
 *
 * The proxy controlling access to the {@link Data}.
 */
@Slf4j
public class Proxy implements UnifiedInterface {

    // 代理个数限制
    private static final int NUM_WIZARDS_ALLOWED = 3;

    // 代理个数计数
    private int number;

    private UnifiedInterface unifiedInterface;

    public Proxy(UnifiedInterface unifiedInterface) {
        this.unifiedInterface = unifiedInterface;
    }

    @Override
    public void consume(Data data) {
        if (number <= NUM_WIZARDS_ALLOWED){
            unifiedInterface.consume(data);
            number ++;
        }else {
            log.info(String.format("%s is not allowed to consume!", data.toString()));
        }
    }
}
